package demo;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class SimulatedLogin {
    public static void main(String[] args) {

        Map<String, String> userInfo = loginUI(); // 模拟前端页面
        boolean result = VerifyUserInfo(userInfo);

        System.out.println(result ? "账号密码正确" : "账号密码错误");
//        String username = "hs";
//        String password = "hbnu";
//
//        for (int i = 0; i < 3; i++) {
//            Scanner sc = new Scanner(System.in);
//            System.out.println("请输入用户名：");
//            String uname = sc.nextLine();
//            System.out.println("请输入密码：");
//            String pwd = sc.nextLine();
//
//            // 判断
//            if (uname.equals(username) && pwd.equals(password)) {
//                System.out.println("登录成功！");
//                break;
//            } else if (i >= 2) {
//                System.out.println("错误次数过多，您的账号已被锁定！！！");
//            } else {
//                System.out.println("登录失败，还有" + (2 - i) + "次机会");
//            }
//        }

    }

    private static Map<String, String> loginUI() {
        Map<String, String> userInfo = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        userInfo.put("username", username);
        userInfo.put("password", password);

        return userInfo;
    }

    // 校验用户登录信息
    private static boolean VerifyUserInfo(Map<String, String> userInfo) {

        boolean flag = false; // 用户信息校验结果

        // 从集合里取出用户信息
        String username = userInfo.get("username");
        String password = userInfo.get("password");

        // 数据库校验用户信息
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        try {
            // 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            String url = "jdbc:mysql://localhost:3306/test2211?serverTimezone=GMT&useSSL=false";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);

            // 获取数据库操作对象
            //statement = connection.createStatement();

            // 执行sql
            String sql = "select * from test2211 where username = ? and password = ?";

            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, username);
            preparedStatement.setString(1, password);

            resultSet = preparedStatement.executeQuery();

            // 处理查询结果
            while ((resultSet.next())) {
                flag = true;
                break;
                }
            } catch(Exception e){
                throw new RuntimeException(e);
            } finally {
                try {
                    if (resultSet != null)
                        resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                try {
                    if (connection != null)
                        connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        return flag;
    }
}
